﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class BoChuyenDoiDienDAO
    {
        public static databaseDataContext _db = new databaseDataContext();

        //Hàm này lấy ra acquy có thông số Ah phù hợp
        public static object lay_danh_sach_bo_kich_dien(double _cs)
        {
            var query = from bkd in _db.BOKICHDIENs
                        where Convert.ToDouble(bkd.CongSuat) >=  _cs
                        select bkd;

            return query.ToList();
        }

        // hàm search bo chuyen doi dien
        public static List<Sanpham_BochuyendoidienDTO> search_sanpham_bochuyendoi(string text)
        {
            List<Sanpham_BochuyendoidienDTO> list = new List<Sanpham_BochuyendoidienDTO>();
            var query = _db.BOKICHDIENs;
            int stt = 1;

            foreach (var item in query)
            {
                Sanpham_BochuyendoidienDTO bochuyendoi = new Sanpham_BochuyendoidienDTO();
                if (item.CongSuat.Contains(text) || item.Loai.Contains(text) || item.GiaTien.Value.ToString() == text)
                {
                    bochuyendoi.stt = stt;
                    bochuyendoi.id = item.MaBoKich;
                    bochuyendoi.congsuat = item.CongSuat;
                    bochuyendoi.loai = item.Loai;
                    bochuyendoi.giatien = item.GiaTien.HasValue ? float.Parse(item.GiaTien.Value.ToString()) : 0;

                    list.Add(bochuyendoi); stt++;
                }
            }

            return list;
        }

        public static void Insert_sanpham_bochuyendoi(Sanpham_BochuyendoidienDTO bochuyen)
        {
            BOKICHDIEN _bokich = new BOKICHDIEN();

            _bokich.CongSuat = bochuyen.congsuat;
            _bokich.Loai = bochuyen.loai;
            _bokich.GiaTien = bochuyen.giatien;

            _db.BOKICHDIENs.InsertOnSubmit(_bokich);
            _db.SubmitChanges();
        }

        public static void Delete_sanpham_bochuyendoidien(int id)
        {
            var bochuyen = _db.BOKICHDIENs.Single(p => p.MaBoKich == id);
            _db.BOKICHDIENs.DeleteOnSubmit(bochuyen);
            _db.SubmitChanges();
        }

        public static Sanpham_BochuyendoidienDTO load_sanpham_bochuyendoi_id(int id)
        {
            Sanpham_BochuyendoidienDTO bochuyen = new Sanpham_BochuyendoidienDTO();
            var query = _db.BOKICHDIENs.Single(p => p.MaBoKich == id);

            bochuyen.congsuat = query.CongSuat;
            bochuyen.loai = query.Loai;
            bochuyen.giatien = query.GiaTien.HasValue ? float.Parse(query.GiaTien.Value.ToString()) : 0;

            return bochuyen;
        }

        public static void Update_sanpham_bochuyendoi(Sanpham_BochuyendoidienDTO bochuyen)
        {
            var item = _db.BOKICHDIENs.Single(p => p.MaBoKich == bochuyen.id);
            item.CongSuat = bochuyen.congsuat;
            item.Loai = bochuyen.loai;
            item.GiaTien = bochuyen.giatien;

            _db.SubmitChanges();
        }






    }
}
